<template>
	<view class="pad20">
		<uni-segmented-control :current="current" :values="zidianList.map(_=>{return _.name})" style-type="button" active-color="#1296db" @clickItem="onClickItem" />
		<view class="pad20">
			<button @click="viewZY" class="colBlue" type="" size="" style="width: 100%; background-color: #fff;">查看全书（左右翻看）</button>
			<button @click="viewSX" class="colBlue top10" type="" size="" style="width: 100%; background-color: #fff">查看全书（上下翻看）</button>
			<button @click="searchGo" class="colBlue top10" type="" size="" style="width: 100%; background-color: #fff">快速检索</button>
			<view class="top20 flex-row-middle">
				<input v-model="pagenum" placeholder="请输入您要查看的页码" type="tel" class="input1 flex1" />
				<view @click="searchDo" class="left10 btn1" style="">
					搜索
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	let t
	const A = getApp()
	export default {
		data() {
			return {
				list: {},
				zidianList: [],
				current: 0,
				imageList: {},
				category_id: '',
				pagenum: ''
			}
		},
		methods: {
			searchDo(){
				if(!/^[0-9]+(\.[0-9]+)?$/.test(t.pagenum)){
					uni.showToast({
						title: `请输入数字`,
						icon: 'none'
					})
					return
				}
				if(t.pagenum > t.list[t.current].length){
					uni.showToast({
						title: `字典页数最大${t.list[t.current].length}`,
						icon: 'none'
					})
					return
				}
				uni.navigateTo({
					url: `/pages/index/fullBook?bookindex=${t.current}&pagenum=${t.pagenum}`
				})
			},
			searchGo(){
				uni.switchTab({
					url: '/pages/index/search'
				})
			},
			onClickItem(e) {
				if (t.current != e.currentIndex) {
					t.current = e.currentIndex;
					t.category_id = t.zidianList[t.current]._id
					t.getList()
				}
			},
			viewZY(){
				uni.previewImage({
					current: t.imageList[t.current][0],
					urls: t.imageList[t.current]
				})
			},
			viewSX(){
				uni.navigateTo({
					url: `/pages/index/fullBook?bookindex=${t.current}`
				})
			},
			getZidian(){
				A.dbRequest({
					name: 'uni-cms-categories',
					field: 'name'
				}).then(_=>{
					t.zidianList = _
					t.category_id = t.zidianList[0]._id
					t.getList()
				})
			},
			getList(){
				A.dbRequest({
					name: 'uni-cms-articles',
					field: 'title,thumbnail',
					where: {
						category_id: t.category_id
					}
				}).then(_=>{
					_.forEach((v, i)=>{
						v.page = i + 1
					})
					t.list[t.current] = _
					A.globalData.fullBook[t.current] = _
					t.imageList[t.current] = _.map(__=>{
						return __.thumbnail[0]
					})
				})
			},
		},
		onLoad() {
			t = this
			t.getZidian()
		}
	}
</script>

<style>
	.container {
		padding: 20px;
		font-size: 14px;
		line-height: 24px;
	}
</style>
